﻿Public Class Form1
    Private cusDTO As CustumersDTO
    Private cusBIZ As CustomerBIZ
    Private cn As CurrencyManager
    Private flag As Boolean

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loaddata()
        lockTextbox(False)
        locButton(True)
    End Sub
    Private Sub loaddata()
        DataGridView1.ClearSelection()
        cusBIZ = New CustomerBIZ
        Dim cusList As New List(Of CustumersDTO)
        cusList = cusBIZ.GetData()
        cn = Me.BindingContext(cusList)
        txtmakh.DataBindings.Add("Text", cusList, "customerId")
        txtho.DataBindings.Add("Text", cusList, "fisrtname")
        txtten.DataBindings.Add("Text", cusList, "lastname")
        txtsodt.DataBindings.Add("Text", cusList, "phone")
        txtdc.DataBindings.Add("Text", cusList, "address")
        DataGridView1.DataSource = cusList

    End Sub
    Private Sub lockTextbox(ByVal f As Boolean)
        txtho.Enabled = f
        txtten.Enabled = f
        txtsodt.Enabled = f
        txtdc.Enabled = f

    End Sub
    Private Sub locButton(ByVal f As Boolean)
        Button1.Enabled = f
        Button2.Enabled = f
        Button3.Enabled = f
        Button4.Enabled = Not f
        Button5.Enabled = Not f

    End Sub
    Private Sub clearText()
        txtmakh.Text = String.Empty
        txtho.Text = " "
        txtten.Text = " "
        txtsodt.Text = " "
        txtdc.Text = " "

    End Sub
    Private Sub clearbinding()
        txtmakh.DataBindings.Clear()
        txtho.DataBindings.Clear()
        txtten.DataBindings.Clear()
        txtsodt.DataBindings.Clear()
        txtdc.DataBindings.Clear()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        flag = True
        lockTextbox(True)
        locButton(False)
        clearText()
        'clearbinding()
        DataGridView1.Enabled = False
        txtho.Focus()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        flag = False
        lockTextbox(True)
        locButton(False)
        DataGridView1.Enabled = False
        txtho.Focus()
    End Sub
    Private Function Testinfo()
        Testinfo = True
        If (Trim(txtho.Text = "")) Then
            Testinfo = False
            MsgBox("Ban chua nhap ho!", MsgBoxStyle.Exclamation, "thong bao")
            txtho.Focus()
            Exit Function
        End If
    End Function

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim cusid As Int32 = Val(txtmakh.Text)
        Dim fistname As String = txtho.Text
        Dim lastname As String = txtten.Text
        Dim phone As String = txtsodt.Text
        Dim address As String = txtsodt.Text
        If Testinfo() Then
            cusDTO = New CustumersDTO(cusid, fistname, lastname, phone, address)
            cusBIZ = New CustomerBIZ()
            cusBIZ.InsertUpdateCustomer(cusDTO, flag)
            clearbinding()
            loaddata()
            lockTextbox(False)
            locButton(True)
            DataGridView1.Enabled = True
            MsgBox("Ban da cap nhat du lieu thanh cong", MsgBoxStyle.Information, "thong bao")
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        clearbinding()
        loaddata()
        lockTextbox(False)
        locButton(True)
        DataGridView1.Enabled = True
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim cusid As String = txtmakh.Text
        If (MsgBox("Ban co muon xoa?", MsgBoxStyle.YesNo, "thong bao") = MsgBoxResult.Yes) Then
            cusDTO = New CustumersDTO(cusid)
            cusBIZ = New CustomerBIZ()
            cusBIZ.DeleteCustumer(cusDTO)
            clearbinding()
            loaddata()
            lockTextbox(False)
            locButton(True)
            MsgBox("ban da xoa thanh cong", MsgBoxStyle.Information, "thong bao")
        Else
            MsgBox("ban da huy thao tac", MsgBoxStyle.Information, "thong bao")
        End If
    End Sub
End Class
